package com.rdc.example.controller;

import com.rdc.example.bean.Result;
import com.rdc.example.bean.dto.ArticleDTO;
import com.rdc.example.bean.dto.CommentDTO;
import com.rdc.example.bean.dto.UserDTO;
import com.rdc.example.service.impl.CommentLikeServiceImpl;
import com.rdc.example.service.impl.CommentServiceImpl;
import com.rdc.example.view.CommentView;

import java.util.List;

import static com.rdc.example.controller.UserController.utilView;

/**
 * @author duck
 */
public class CommentController {
    private static final CommentServiceImpl commentServial = new CommentServiceImpl();

    public static void main(String[] args) {
        insertComment(1,1);
    }


    public static void insertComment(Integer userId, Integer articleId){
        if (userId==null){
            utilView.showError("Id is null");
            return;
        }
        if (articleId==null){
            utilView.showError("article is null");
        }
        try{
            CommentDTO commentDTO = new CommentDTO();
            CommentView.insertComment(commentDTO);
            commentDTO.setArticleId(articleId);
            commentDTO.setUserId(userId);
            Result result =commentServial.insertComment(commentDTO);
            System.out.println(result.getMessage());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void deleteComment(UserDTO currentUserDTO, ArticleDTO currentArticleDTO, List<CommentDTO> currentCommentDTOList) {
        //检测权限：是否为自己的评论，是否为作者的评论，是否为管理者的评论
        try {
            Integer index= utilView.getUserInputInteger("请输入评论编号： ");
            if (index<1||index>currentCommentDTOList.size()){
                utilView.showMessage("对应评论不存在！");
                return;
            }
            CommentDTO commentDTO = currentCommentDTOList.get(index-1);
            if(commentDTO.getUserId().equals(currentUserDTO.getId())||currentArticleDTO.getUserId().equals(currentUserDTO.getId())||currentUserDTO.getPower()){
                Result result =commentServial.deleteComment(commentDTO.getId());
                System.out.println(result.getMessage());
            }
            else{
                utilView.showMessage("权限不足");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void turnCommentLike( List<CommentDTO> currentCommentDTOList,Integer userId) {
        if (userId==null){
            utilView.showError("Id is null");
            return;
        }
        if (currentCommentDTOList==null){
            utilView.showError("currentCommentDTOList is null");
        }
        try {
            Integer index= utilView.getUserInputInteger("请输入评论编号： ");
            if (currentCommentDTOList != null && (index < 1 || index > currentCommentDTOList.size())) {
                utilView.showMessage("对应评论不存在！");
                return;
            }
            CommentDTO commentDTO = null;
            if (currentCommentDTOList != null) {
                commentDTO = currentCommentDTOList.get(index-1);
            }
            CommentLikeServiceImpl commentLikeServiceImpl = new CommentLikeServiceImpl();
            Result result =commentLikeServiceImpl.turnCommentLike(userId,commentDTO.getId());
            System.out.println(result.getMessage());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
